<?php

namespace ApiWF\Controller;

use Common\Controller\ApibaseController;
use Common\Controller\Std3Des;

class ParkController extends ApibaseController
{
    protected $user_name;
    protected $user_pwd;
    protected $user_key;
    protected $nowtime;
    protected $headers;
    protected $commdata;

    public function __construct()
    {
        $nowtime = date("Ymd",time());

        $interface_setting = D('Options')->where(['option_name' => 'park_settings'])->find();
        $user = json_decode($interface_setting['option_value'],true);
        $this->user_name = $user['user_name'];
        $this->user_pwd = $user['user_pwd'];
        $this->user_key = base64_encode($user['user_key']);
        $this->user_iv = base64_encode($nowtime);
        $this->commdata = '';

        $headers = array(
            'Content-Type' => 'application/json',
            'charset'    => 'utf-8'
        );

        $headers['user'] = $user['user_name'];
        $headers['pwd'] = $user['user_pwd'];

        $this->headers = $headers;
    }


    public function index(){
        $url='http://220.160.111.114:9099/api/find/GetParkingLotInfo';
        $result = $this->curl($url,$this->headers,$this->commdata);

        $res = json_decode($result,true);

        $this->jsonReturn($res);
    }

    public function GetParkingLotInfo(){
        $interface = D('Interface')->where(['id' => '1'])->find();
        $url = $interface['inter_url'];
        $result = $this->curl($url,$this->headers,$this->commdata);

        $res = json_decode($result,true);

        if(!empty($res['data'])){
            $inter['inter_status'] = '1';
        }else{
            $inter['inter_status'] = '0';
        }

        D('Interface')->where(['id' => '1'])->save($inter);

        $this->jsonReturn($res);
    }

    public function GetFloorList(){
        $interface = D('Interface')->where(['id' => '3'])->find();
        $url = $interface['inter_url'];
        $result = $this->curl($url,$this->headers,$this->commdata);

        $res = json_decode($result,true);

        if(!empty($res['data'])){
            $inter['inter_status'] = '1';
        }else{
            $inter['inter_status'] = '0';
        }

        D('Interface')->where(['id' => '3'])->save($inter);

        $this->jsonReturn($res);
    }

    public function GetAreaList(){
        $floorId = I('floorId','0',intval);

        $dataArr = array(
            'floorId'=>$floorId,
            );

        $floorId = json_encode($dataArr,true);

        $obj = new STD3Des($this->user_key,$this->user_iv);
        $postdata = $obj->encrypt($floorId);

        $post_data = array(
            'data'=>$postdata
            );
        $this->commdata =json_encode($post_data,true);

        $interface = D('Interface')->where(['id' => '4'])->find();
        $url = $interface['inter_url'];

        $result = $this->curl($url,$this->headers,$this->commdata);

        $res = json_decode($result,true);

        if(!empty($res['data'])){
            $inter['inter_status'] = '1';
        }else{
            $inter['inter_status'] = '0';
        }

        D('Interface')->where(['id' => '4'])->save($inter);

        $this->jsonReturn($res);
    }

    public function GetCarLocInfo(){
        $plateNo = I('plateNo','0',intval);

        $dataArr = array(
            'plateNo'=>$plateNo,
            );

        $plateNo = json_encode($dataArr,true);

        $obj = new STD3Des($this->user_key,$this->user_iv);
        $postdata = $obj->encrypt($plateNo);

        $post_data = array(
            'data'=>$postdata
            );
        $this->commdata =json_encode($post_data,true);

        $interface = D('Interface')->where(['id' => '5'])->find();
        $url = $interface['inter_url'];

        $result = $this->curl($url,$this->headers,$this->commdata);

        $res = json_decode($result,true);

        if(!empty($res['data'])){
            $inter['inter_status'] = '1';
        }else{
            $inter['inter_status'] = '0';
        }

        D('Interface')->where(['id' => '5'])->save($inter);

        $this->jsonReturn($res);
    }

    public function GetCarLocList(){
        $plateNo = I('plateNo','0',intval);
        $count = I('count','1',intval);

        $dataArr = array(
            'plateNo'=>$plateNo,
            'count'=>$count,
        );

        $plateNo = json_encode($dataArr,true);

        $obj = new STD3Des($this->user_key,$this->user_iv);
        $postdata = $obj->encrypt($plateNo);

        $post_data = array(
            'data'=>$postdata
            );
        $this->commdata =json_encode($post_data,true);

        $interface = D('Interface')->where(['id' => '6'])->find();
        $url = $interface['inter_url'];

        $result = $this->curl($url,$this->headers,$this->commdata);

        $res = json_decode($result,true);

        if(!empty($res['data'])){
            $inter['inter_status'] = '1';
        }else{
            $inter['inter_status'] = '0';
        }

        D('Interface')->where(['id' => '6'])->save($inter);

        $this->jsonReturn($res);
    }


    public function GetCarLocList2(){
        $plateNo = I('plateNo','0',intval);
        $pageIndex = I('pageIndex','1',intval);
        $pageSize = I('count','3',intval);

        $dataArr = array(
            'plateNo'=>$plateNo,
            'pageIndex'=>$pageIndex,
            'pageSize'=>$pageSize,
        );

        $plateNo = json_encode($dataArr,true);

        $obj = new STD3Des($this->user_key,$this->user_iv);
        $postdata = $obj->encrypt($plateNo);

        $post_data = array(
            'data'=>$postdata
            );
        $this->commdata =json_encode($post_data,true);

        $interface = D('Interface')->where(['id' => '7'])->find();
        $url = $interface['inter_url'];

        $result = $this->curl($url,$this->headers,$this->commdata);

        $res = json_decode($result,true);

        if(!empty($res['data'])){
            $inter['inter_status'] = '1';
        }else{
            $inter['inter_status'] = '0';
        }

        D('Interface')->where(['id' => '7'])->save($inter);

        $this->jsonReturn($res);
    }

    public function GetFreeSpaceNum(){
        $floorId = I('floorId','0',intval);
        $areaId = I('areaId','0',intval);

        $dataArr = array(
            'floorId'=>$floorId,
            'areaId'=>$areaId,
        );

        $plateNo = json_encode($dataArr,true);

        $obj = new STD3Des($this->user_key,$this->user_iv);
        $postdata = $obj->encrypt($plateNo);

        $post_data = array(
            'data'=>$postdata
            );
        $this->commdata =json_encode($post_data,true);

        $interface = D('Interface')->where(['id' => '8'])->find();
        $url = $interface['inter_url'];

        $result = $this->curl($url,$this->headers,$this->commdata);

        $res = json_decode($result,true);

        if(!empty($res['data'])){
            $inter['inter_status'] = '1';
        }else{
            $inter['inter_status'] = '0';
        }

        D('Interface')->where(['id' => '8'])->save($inter);

        $this->jsonReturn($res);
    }








    private function curl($url,$headers,$postdata){
        $ch = curl_init($url);
        $headers = array("Content-Type: application/json", "user: ktapi", "pwd: 0306A9");
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        $result = curl_exec($ch);
        // curl_close($ch);
        return $result;
    }
}

